package com.nf.practice.downloadFile;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;

public class FileDownloader {
    private static final int THREAD_POOL_SIZE = 10;
    private static final int BATCH_SIZE = 200;
    private static final String DOWNLOAD_DIRECTORY = "download/";

    public static void main(String[] args) {
        List<String> noList = getNoList(); // 获取单号列表

        ExecutorService executorService = Executors.newFixedThreadPool(THREAD_POOL_SIZE);
        List<Future<Boolean>> futures = new ArrayList<>();

        for (int i = 0; i < noList.size(); i += BATCH_SIZE) {
            int endIndex = Math.min(i + BATCH_SIZE, noList.size());
            List<String> batchNoList = noList.subList(i, endIndex);

            Callable<Boolean> task = new DownloadTask(batchNoList);
            Future<Boolean> future = executorService.submit(task);
            futures.add(future);
        }

        // 等待所有任务完成
        for (Future<Boolean> future : futures) {
            try {
                future.get();
            } catch (InterruptedException | ExecutionException e) {
                e.printStackTrace();
            }
        }

        executorService.shutdown();

        // 压缩目录和文件
        compressDirectory(DOWNLOAD_DIRECTORY);
    }

    private static List<String> getNoList() {
        // 获取单号列表的逻辑
        // ...

        return new ArrayList<>();
    }

    private static void downloadFile(String no) {
        // 下载文件的逻辑
        // ...
    }

    private static void compressDirectory(String directory) {
        // 压缩目录和文件的逻辑
        // ...
    }

    private static class DownloadTask implements Callable<Boolean> {

        private List<String> batchNoList;

        public DownloadTask(List<String> batchNoList) {
            this.batchNoList = batchNoList;
        }

        @Override
        public Boolean call() {
            for (String appNo : batchNoList) {
                downloadFile(appNo);
            }
            return true;
        }
    }
}